package fr.aprr.geoloc.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.TextField;

import fr.aprr.geoloc.client.presenser.interfaces.ITabView;
import fr.aprr.geoloc.client.service.OdysseeService;
import fr.aprr.geoloc.client.service.OdysseeServiceAsync;
import fr.aprr.geoloc.shared.domain.Odyssee;

public class TabPaneView extends Composite implements ITabView {

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */


	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final OdysseeServiceAsync odysseeService = GWT.create(OdysseeService.class);
	
	VerticalLayoutContainer secondLayout = new VerticalLayoutContainer();
	TabPanel tabPanel = new TabPanel();
	
	public TabPaneView() {

		final TextButton sendButton = new TextButton("Search");
		final TextField nameField = new TextField();
		nameField.setText("Data");
		final Label errorLabel = new Label();

		MainPanel mainPanel = new MainPanel();

		secondLayout.add(nameField);
		secondLayout.add(sendButton);
		secondLayout.add(errorLabel);

		
		tabPanel.setAnimScroll(true);
		tabPanel.setTabScroll(true);
		tabPanel.setCloseContextMenu(true);
		tabPanel.add(mainPanel, "Dijon");
	
		tabPanel.add(secondLayout, new TabItemConfig("Paris", true));

		// Set Configuration for Table
		
		// Create the popup dialog box
		final Dialog dialogBox = new Dialog();
		dialogBox.setBodyStyleName("pad-text");
		dialogBox.setHideOnButtonClick(true);
		dialogBox.setHeadingText("Remote Procedure Call");
		// dialogBox.setAnimationEnabled(true);
		final TextButton closeButton = new TextButton("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();

		// Vertical Layout Container
		VerticalLayoutContainer layout = new VerticalLayoutContainer();
		dialogBox.setWidget(layout);

		ContentPanel dialogVPanel = new ContentPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.add(closeButton);

		// Add a handler to close the DialogBox
		closeButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				// TODO Auto-generated method stub
				dialogBox.hide();
				sendButton.setEnabled(true);
			}
		});

		// Create a handler for the sendButton and nameField
		class MyHandler implements SelectHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onSelect(SelectEvent event) {
				// TODO Auto-generated method stub
				sendNameToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendNameToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void sendNameToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				String textToServer = nameField.getText();

				// Then, we send the input to the server.
				sendButton.setEnabled(true);
				textToServerLabel.setText(textToServer);
				serverResponseLabel.setText("");
							
				odysseeService.getOdyssee(3, new AsyncCallback<Odyssee>() {

					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub
						dialogBox.add(new Label(
								"Remote Procedure Call - Failure"));
						serverResponseLabel
								.addStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML("Error");
						dialogBox.center();
					}

					public void onSuccess(Odyssee odyssee) {
						// TODO Auto-generated method stub
						dialogBox
						.add(new Label("Remote Procedure Call"));
				serverResponseLabel
						.removeStyleName("serverResponseLabelError");
				serverResponseLabel.setHTML(odyssee.getNomChamp());
				dialogBox.add(serverResponseLabel);
				dialogBox.center();
				dialogBox.show();
					}
					
				});
			}

		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addSelectHandler(handler);
		nameField.addKeyUpHandler(handler);

		initWidget(tabPanel);
	}

	public void addWidget2Layout(IsWidget view) {
		// TODO Auto-generated method stub
		secondLayout.add(view);
	}

	public void addNewTab(String name) {
		// TODO Auto-generated method stub
		tabPanel.add(new Button("AAA"), new TabItemConfig(name, true));
	}

}
